using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Manazra.Controllers.Filters;
using Manazra.Models.Repository;
using Manazra.Models;
using Manazra.Helpers;

namespace Manazra.Controllers
{
    //viewmodel for author page
    public class AuthorViewModel
    {
        //properties
        public string Author { get; private set; }
        public PaginatedList<Article> Articles { get; private set; }

        //construstor
        public AuthorViewModel(string author, PaginatedList<Article> articles)
        {
            Author = author;
            Articles = articles;
        }
    }

    [BasePreControllerActionFilter]
    public class AuthorsController : Controller
    {
        //articles repository
        private ArticleRepository articleRep = new ArticleRepository();

        private const int listingPageSize = 10;
        
        //authors information page
        public ActionResult Index(string id, int? page)
        {
            //get list of articles
            var articles = articleRep.GetByAuthor(id);
            //apply paging
            var paginatedArticles = new PaginatedList<Article>(articles, page ?? 0, listingPageSize);
            //show view
            return View(new AuthorViewModel(id, paginatedArticles));
        }

        //authors information page
        public ActionResult Popular(string id, int? page)
        {
            //get list of articles
            var articles = articleRep.GetByAuthorAndPopular(id);
            //apply paging
            var paginatedArticles = new PaginatedList<Article>(articles, page ?? 0, listingPageSize);
            //show view
            return View("Index", new AuthorViewModel(id, paginatedArticles));
        }
    }
}
